<template>
	<view class="home">
		<!-- 自定义导航栏组件 -->
		<navbar></navbar>
		<!-- 自定义选项卡组件 -->
		<tab :list='labelList' :swiperIndex='swiperIndex' @tab='tab'></tab>
		<!-- 自定义轮播图组件, 使用轮播图实现 --> 
		<view class="home-swiper">
			<swiper-list :tabLsit='labelList' :tabIndex='tabIndex' @swiperListChange='swiperListChange'></swiper-list>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
				labelList: [],
				swiperIndex:0,
				tabIndex:0
			}
		},
		computed:{
			...mapState(['userinfo'])
		},
		watch:{
			userinfo(newVal){
				this.getLabel()
			}
		},
		onLoad() {
			uni.$on('updateLabel',(res=>{
				this.getLabel()
				this.labelList = []
				this.swiperIndex = 0
				this.tabIndex = 0
			}))
			this.getLabel()
		},
		methods: {
			getLabel() {
				this.$api.getLabel().then(res => {
					let {data} = res
					data.unshift({
						name:'全部'
					})
					this.labelList = data
				})
			},
			tab({item,index}) {
				this.tabIndex = index
			},
			swiperListChange(current){
				this.swiperIndex = current
				this.tabIndex = current
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		display: flex;
		height: 100%;
	}
	.home {
		display: flex;
		flex-direction: column;
		overflow: hidden;
		flex:1;
		.home-swiper{
			flex: 1;
			// overflow: hidden;
			// box-sizing: border-box;
		}
	}
</style>
